package com.zhf.cloud.gateway.springcloudgateway.model.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhf.cloud.gateway.springcloudgateway.entity.UserInfo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;

/**
 * @author: 曾鸿发
 * @create: 2021-11-22 11:53
 * @description：spring security当前登录对象
 **/
@Getter
@Setter
public class LoginAppUser3 extends UserInfo implements UserDetails {

    private static final long serialVersionUID = 1753977564987556640L;

    //private Set<SysRole> sysRoles;

    //private Set<String> permissions;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
//		if (!CollectionUtils.isEmpty(sysRoles)) {
//			sysRoles.forEach(role -> {
//				if (role.getCode().startsWith("ROLE_")) {
//					collection.add(new SimpleGrantedAuthority(role.getCode()));
//				} else {
//					collection.add(new SimpleGrantedAuthority("ROLE_" + role.getCode()));
//				}
//			});
//		}
//
//		if (!CollectionUtils.isEmpty(permissions)) {
//			permissions.forEach(per -> {
//				collection.add(new SimpleGrantedAuthority(per));
//			});
//		}

        return collection;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if(getEnabled() == true && getStatus() == true)
            return true;
        return false;
    }
}
